package cn.e4j.modular.system.service.impl;

import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.system.entity.SysUserDevice;
import cn.e4j.modular.system.entity.SysUserRole;
import cn.e4j.modular.system.mapper.SysUserDeviceMapper;
import cn.e4j.modular.system.service.SysUserDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备组模块-用户关联表 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-02-15
 */
@Service
public class SysUserDeviceServiceImpl extends ServiceImpl<SysUserDeviceMapper, SysUserDevice> implements SysUserDeviceService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, String>> selectGwaeidsByUser(String Userid) {
        return this.baseMapper.selectGwaeidsByUser(Userid);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, String>> selectUseridsByRole(String Roleid) {
        return this.baseMapper.selectUseridsByRole(Roleid);
    }

//    @Transactional
//    @Override
//    public void addUserGwaes(List<SysUserDevice> lists) {
//        for (SysUserDevice sysUserDevice : lists) {
//            this.baseMapper.insert(sysUserDevice);
//        }
//    }
//
//    @Transactional
//    @Override
//    public void delUserGwaes(List<SysUserDevice> lists) {
//        for (SysUserDevice sysUserDevice : lists) {
//            if (ToolUtil.isNotEmpty(sysUserDevice.getUSER_ID()) && ToolUtil.isNotEmpty(sysUserDevice.getDEVICE_ID())) {
//                Map<String, Object> map = new HashedMap();
//                map.put("USER_ID", sysUserDevice.getUSER_ID());
//                map.put("DEVICE_ID", sysUserDevice.getDEVICE_ID());
//                this.baseMapper.deleteByMap(map);
//            }
//        }
//    }

    @Transactional
    @Override
    public void addUserGwbws(List<SysUserDevice> lists) {
        for (SysUserDevice sysUserDevice : lists) {
            this.baseMapper.insert(sysUserDevice);
        }
    }

    @Transactional
    @Override
    public void delUserGwbws(List<SysUserDevice> lists) {
        for (SysUserDevice sysUserDevice : lists) {
            if (ToolUtil.isNotEmpty(sysUserDevice.getUSER_ID()) && ToolUtil.isNotEmpty(sysUserDevice.getDEVICE_ID())) {
                Map<String, Object> map = new HashedMap();
                map.put("USER_ID", sysUserDevice.getUSER_ID());
                map.put("DEVICE_ID", sysUserDevice.getDEVICE_ID());
                this.baseMapper.deleteByMap(map);
            }
        }
    }
}
